package com.gitee.cirnochat.group.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.cirnochat.group.mapper.GroupMapper;
import com.gitee.cirnochat.group.model.entity.Group;
import com.gitee.cirnochat.group.model.entity.GroupMember;
import com.gitee.cirnochat.group.model.vo.req.GroupCreateRequest;
import com.gitee.cirnochat.group.model.vo.resp.GroupVo;
import com.gitee.cirnochat.group.service.GroupMemberService;
import com.gitee.cirnochat.group.service.GroupService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
* @author FlanChan
* @description 针对表【group(群聊信息)】的数据库操作Service实现
* @createDate 2025-04-17 14:31:33
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group>
    implements GroupService {

    private final GroupMemberService groupMemberService;

    @Override
    public GroupVo createGroup(GroupCreateRequest createRequest, MultipartFile file, HttpServletRequest request) {
        log.info("Creating group with request: {}", createRequest);
        log.info("File: {}", file);
        return null;
    }

    @Override
    public Group getOneByUUID(String uuid) {
        return this.getOne(new LambdaQueryWrapper<>(Group.class).eq(Group::getGroupUuid,uuid));
    }

    @Override
    public boolean existsById(Long groupId) {
        return this.getOne(new LambdaQueryWrapper<>(Group.class)
                .eq(Group::getId, groupId)) != null;
    }

    @Override
    public void updateGroupAvatar(Long groupId, String avatarUri) {
        this.update(new LambdaUpdateWrapper<>(Group.class)
                .eq(Group::getId, groupId)
                .set(Group::getGroupAvatar, avatarUri));
    }

    @Override
    public List<Long> getGroupMemberIdsBySessionId(Long sessionId) {
        return groupMemberService.list(new LambdaQueryWrapper<>(GroupMember.class)
                .eq(GroupMember::getGroupId, sessionId))
                .stream()
                .map(GroupMember::getMemberId)
                .toList();
    }
}




